package com.flyou.ccnucenter.ui.activity.login;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.MyUser;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.Md5Parser;
import com.flyou.ccnucenter.utils.RegexPatternUtils;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.utils.TextEmptyUtil;
import com.flyou.ccnucenter.widget.ClearEditText;

import org.simple.eventbus.EventBus;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

public class LoginActivity extends BaseActivity1 implements View.OnClickListener {
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private ClearEditText mLoginEtPhone;
    private ClearEditText mLoginEtPassword;
    private Button mLoginBtLogin;
    private TextView mLoginTvRegist;
    private TextView mLoginTvForgetPassword;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);

        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);

        mLoginEtPhone = (ClearEditText) findViewById(R.id.login_et_phone);
        mLoginEtPassword = (ClearEditText) findViewById(R.id.login_et_password);
        mLoginBtLogin = (Button) findViewById(R.id.login_bt_login);
        mLoginTvRegist = (TextView) findViewById(R.id.login_tv_regist);
        mLoginTvForgetPassword = (TextView) findViewById(R.id.login_tv_forget_password);
    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("登录");

    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);

        mLoginBtLogin.setOnClickListener(this);
        mLoginTvRegist.setOnClickListener(this);
        mLoginTvForgetPassword.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                finish();
                break;

            case R.id.login_bt_login:

                if (mLoginEtPhone.getText().toString().trim().isEmpty() || mLoginEtPassword.getText().toString().trim().isEmpty()) {
                    Toast.makeText(this, R.string.regex_password_username_empty, Toast.LENGTH_SHORT).show();
                    return;
                }
//                if (!RegexPatternUtils.isMobile(mLoginEtPhone.getText().toString().trim())) {
//                    Toast.makeText(this, R.string.regex_phone_error, Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (!RegexPatternUtils.isPassword(mLoginEtPassword.getText().toString().trim())) {
                    Toast.makeText(this, R.string.regex_password_error, Toast.LENGTH_SHORT).show();
                    return;
                }
                mProgressDialog = ProgressDialog.show(LoginActivity.this, null, "正在登录", false, false);

                MyUser myUser = new MyUser();
                myUser.loginByAccount(LoginActivity.this, mLoginEtPhone.getText().toString().trim(), Md5Parser.md5Parser(mLoginEtPassword.getText().toString().trim()), new LogInListener<MyUser>() {
                    @Override
                    public void done(MyUser myUser, BmobException e) {
                        mProgressDialog.dismiss();
                        if (myUser != null) {
                            Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
//                            通知主界面更新头像
                            EventBus.getDefault().post("", Constant.UPDATE_HEADIMAGE);
                            SPUtils.put(LoginActivity.this, "userName", myUser.getUsername());
                            SPUtils.put(LoginActivity.this, "phone", myUser.getMobilePhoneNumber());

                            SPUtils.put(LoginActivity.this, "email", TextEmptyUtil.SetEmptyTo( myUser.getEmail()));
                            SPUtils.put(LoginActivity.this, "headImage", myUser.getHeadImage());
                            SPUtils.put(LoginActivity.this, "objectId", myUser.getObjectId());
                            SPUtils.put(LoginActivity.this, "grade", TextEmptyUtil.SetEmptyTo(myUser.getGrade()));
                            SPUtils.put(LoginActivity.this, "department", TextEmptyUtil.SetEmptyTo(myUser.getDepartment()));
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "用户名密码不匹配，请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            case R.id.login_tv_regist:
                Intent registIntent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(registIntent);
                break;
            case R.id.login_tv_forget_password:
                Intent forGetPasswoedIntent = new Intent(LoginActivity.this, GetBackPasswordActivity.class);

                startActivity(forGetPasswoedIntent);
                break;

        }
    }
}
